package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    /**
     *新增套餐
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealRedis" ,key = "#setmealDto.categoryId")
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.setmealWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){

        Page<SetmealDto> pageInfo= setmealService.pageWithDto(page,pageSize,name);
        return R.success(pageInfo);
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealRedis" ,allEntries = true)
    public R<String> delete(Long[] ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 批量起售
     */
    @PostMapping("/status/{status}")
    @Transactional
    @CacheEvict(cacheNames = "setmealRedis" ,allEntries = true)
    public R<String> updateStatus(@PathVariable int status,Long[] ids){
        Setmeal setmeal=new Setmeal();
            setmeal.setStatus(status);
        for (Long id : ids) {
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }
        return R.success("批售成功");
    }

    /**
     * 回显
     */
    @GetMapping("{id}")
    public R<SetmealDto> show(@PathVariable Long id){

       SetmealDto setmealDto= setmealService.showSetmealDto(id);
       return R.success(setmealDto);
    }

    /**
     * 修改
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealRedis" ,allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     *根据条件查询套餐数据
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "setmealRedis",key = "#setmeal.categoryId")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus())
                .eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
   }
}
